/* eslint-disable max-statements */ import type { ParsedUrlQuery } from 'querystring'; import type { GetStaticPaths, GetStaticProps } from 'next'; import Head from 'next/head'; import { useRouter } from 'next/router'; import Script from 'next/script'; import { useCallback } from 'react'; import { useIntl } from 'react-intl'; import { getLayout, Heading, LinksWidget, type MetaItemData, PageLayout, PostsList, Pagination, type RenderPaginationLink, type RenderPaginationItemAriaLabel, } from '../../../components'; import { getArticles, getArticlesEndCursor, getThematicsPreview, getTopicsPreview, getTotalArticles, getTotalThematics, getTotalTopics, } from '../../../services/graphql'; import type { EdgesResponse, NextPageWithLayout, RawArticle, RawThematicPreview, RawTopicPreview, } from '../../../types'; import { CONFIG } from '../../../utils/config'; import { ROUTES } from '../../../utils/constants'; import { getBlogSchema, getLinksItemData, getPageLinkFromRawData, getPostsList, getSchemaJson, getWebPageSchema, } from '../../../utils/helpers'; import { loadTranslation, type Messages } from '../../../utils/helpers/server'; import { useBreadcrumb, useRedirection } from '../../../utils/hooks'; type BlogPageProps = { articles: EdgesResponse; pageNumber: number; thematicsList: RawThematicPreview[]; topicsList: RawTopicPreview[]; totalArticles: number; translation: Messages; }; /** * Blog index page. */ const BlogPage: NextPageWithLayout = ({ articles, pageNumber, thematicsList, topicsList, totalArticles, }) => { useRedirection({ query: { param: 'number', value: '1' }, redirectTo: ROUTES.BLOG, }); const intl = useIntl(); const title = intl.formatMessage({ defaultMessage: 'Blog', description: 'BlogPage: page title', id: '7TbbIk', }); const pageNumberTitle = intl.formatMessage( { defaultMessage: 'Page {number}', id: 'zbzlb1', description: 'BlogPage: page number', }, { number: pageNumber, } ); const pageTitleWithPageNumber = `${title} - ${pageNumberTitle}`; const { items: breadcrumbItems, schema: breadcrumbSchema } = useBreadcrumb({ title: pageNumberTitle, url: `${ROUTES.BLOG}/page/${pageNumber}`, }); const { asPath } = useRouter(); const page = { title: `${pageTitleWithPageNumber} - ${CONFIG.name}`, url: `${CONFIG.url}${asPath}`, }; const pageDescription = intl.formatMessage( { defaultMessage: "Discover {websiteName}'s writings. He talks about web development, Linux and open source mostly.", description: 'BlogPage: SEO - Meta description', id: '18h/t0', }, { websiteName: CONFIG.name } ); const webpageSchema = getWebPageSchema({ description: pageDescription, locale: CONFIG.locales.defaultLocale, slug: asPath, title, }); const blogSchema = getBlogSchema({ isSinglePage: false, locale: CONFIG.locales.defaultLocale, slug: asPath, }); const schemaJsonLd = getSchemaJson([webpageSchema, blogSchema]); const thematicsListTitle = intl.formatMessage({ defaultMessage: 'Thematics', description: 'BlogPage: thematics list widget title', id: 'HriY57', }); const topicsListTitle = intl.formatMessage({ defaultMessage: 'Topics', description: 'BlogPage: topics list widget title', id: '2D9tB5', }); const renderPaginationLink: RenderPaginationLink = useCallback( (pageNum) => `${ROUTES.BLOG}/page/${pageNum}`, [] ); const renderPaginationLabel: RenderPaginationItemAriaLabel = useCallback( ({ kind, pageNumber: number, isCurrentPage }) => { switch (kind) { case 'backward': return intl.formatMessage( { defaultMessage: 'Go to previous page, page {number}', description: 'BlogPage: previous page label', id: 'faO6BQ', }, { number } ); case 'forward': return intl.formatMessage( { defaultMessage: 'Go to next page, page {number}', description: 'BlogPage: next page label', id: 'oq3BzP', }, { number } ); case 'number': default: return isCurrentPage ? intl.formatMessage( { defaultMessage: 'Current page, page {number}', description: 'BlogPage: current page label', id: 'JL6G22', }, { number } ) : intl.formatMessage( { defaultMessage: 'Go to page {number}', description: 'BlogPage: page number label', id: 'IVczxR', }, { number } ); } }, [intl] ); const headerMeta: MetaItemData[] = totalArticles ? [ { id: 'posts-count', label: intl.formatMessage({ defaultMessage: 'Total:', description: 'Page: total label', id: 'kNBXyK', }), value: intl.formatMessage( { defaultMessage: '{postsCount, plural, =0 {No articles} one {# article} other {# articles}}', description: 'Page: posts count meta', id: 'RvGb2c', }, { postsCount: totalArticles } ), }, ] : []; const paginationAriaLabel = intl.formatMessage({ defaultMessage: 'Pagination', description: 'BlogPage: pagination accessible name', id: 'AXe1Iz', }); return ( <> {page.title} {/*eslint-disable-next-line react/jsx-no-literals -- Name allowed */} {/*eslint-disable-next-line react/jsx-no-literals -- Content allowed */}